using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace CSShell
{
    class Plugin : TabControl
    {
        public Plugin()         
        {
            Init();
        }

        public Size GetFirstInnerFormSize()
        {
            return firstInnerFormSize;
        }

        private void Init()
        {
            try
            {
                ArrayList _plugins = new ArrayList(); 
                SearchPlugin(ref _plugins);

                int addIndex = 0;
                foreach (string _path in _plugins)
                {
                    try
                    {
                        Assembly _ass = Assembly.LoadFile(_path);
                        Type[] _types = _ass.GetTypes();
                        foreach (Type _type in _types)
                        {
                            if (_type.BaseType != null && _type.BaseType.Name == "Form")
                            {
                                Form _form = (Form)Activator.CreateInstance(_type);

                                if (_form.FormBorderStyle == FormBorderStyle.None)
                                {
                                    TabPage _tabpage = new TabPage(_form.Text);
                                    _form.TopLevel = false;
                                    _form.Show();
                                    _tabpage.Controls.Add(_form);
                                    this.TabPages.Add(_tabpage);

                                    // Get the first tab page's inner form's size width and height.
                                    if (addIndex == 0)
                                    {
                                        firstInnerFormSize = _form.Size;
                                    }
                                    addIndex++;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        string _err = e.Message;
                    }
                }
            }
            catch (Exception e)
            {
                string _err = e.Message;
            }
        }

        private void SearchPlugin(ref ArrayList _plugins)
        {
            _plugins.Clear();

            try
            {
                string _path = Application.StartupPath;
                DirectoryInfo _dir = new DirectoryInfo(_path);
                FileInfo[] _files = _dir.GetFiles("*.dll");
                foreach (FileInfo _file in _files)
                {
                    _plugins.Add(_file.FullName);
                }
            }
            catch (Exception e)
            {
                string _err = e.Message;
                throw e;
            }
        }

        private Size firstInnerFormSize = new Size();
    }
}
